package com.zfj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfj.entity.EduChapter;
import com.zfj.entity.EduVideo;
import com.zfj.entity.chapter.ChapterVo;
import com.zfj.entity.chapter.VideoVo;
import com.zfj.entity.vo.CoursePublishVo;
import com.zfj.handler.GuLiException;
import com.zfj.mapper.EduChapterMapper;
import com.zfj.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfj.service.EduVideoService;
import com.zfj.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zfj
 * @since 2022-10-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
        //根据课程id查询所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);
        //创建list用于最后的封装
        List<ChapterVo> finalList=new ArrayList<>();
        //遍历list进行封装
        //遍历章节
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter复制到ChapterVo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);
            List<VideoVo> videoList = new ArrayList<>();
            //遍历小结
            for (int m = 0; m < eduVideoList.size(); m++) {
                EduVideo eduVideo = eduVideoList.get(m);
                //判断小节里面的chapterId和章节id知否一致
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }

            }
            //把封装后的小节放到章节对象里
            chapterVo.setChildren(videoList);
        }

        return finalList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        // 查询该章节下是否有小节, 有则不能删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if (count > 0){
                throw new GuLiException(20001, "不能删除章节");
        }else {
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

}
